package com.sqsf.text;
import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class MyTest {
    @Test
    public void springDruid() throws Exception {
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        DruidDataSource druid= (DruidDataSource) applicationContext.getBean("druid");
        Connection connection = druid.getConnection();
        System.out.println(connection);
        connection.close();
        applicationContext.close();
    }
    @Test
    public void springC3p0() throws Exception {
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        ComboPooledDataSource c3p0= (ComboPooledDataSource) applicationContext.getBean("c3p0");
        Connection connection = c3p0.getConnection();
        System.out.println(connection);
        connection.close();
        applicationContext.close();
    }
    @Test
    public void druidTest() throws Exception {
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");
        //使用无参构造方法创建druid连接池对象
        DruidDataSource druid=new DruidDataSource();
        //通过set方法进行赋值
        druid.setDriverClassName(rb.getString("jdbc.driver"));
        druid.setUrl(rb.getString("jdbc.url"));
        druid.setUsername(rb.getString("jdbc.username"));
        druid.setPassword(rb.getString("jdbc.password"));
        //获取连接对象
        Connection connection = druid.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        druid.close();
    }
    @Test
    public void c3p0Test() throws Exception {
        //在实际开发中我们经常将数据库的连接配置书写在properties配置文件中,
        //所以参数不应该写死 而是应该从properties配置文件中提取
        //java中提供了各种获取properties的方式 其中ResourceBundle是最简化的形式
        //专门用于提取properties配置文件的 仅书写文件名即可 会自动读取classpath下的对应文件
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");


        //使用无参构造方法创建c3p0连接池对象
        ComboPooledDataSource c3p0=new ComboPooledDataSource();
        //通过set方法进行赋值
        c3p0.setDriverClass(rb.getString("jdbc.driver"));
        c3p0.setJdbcUrl(rb.getString("jdbc.url"));
        c3p0.setUser(rb.getString("jdbc.username"));
        c3p0.setPassword(rb.getString("jdbc.password"));
        //获取连接对象
        Connection connection = c3p0.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        c3p0.close();
    }
    @Test
    public void druid() throws Exception {
        //使用无参构造方法创建druid连接池对象
        DruidDataSource druid=new DruidDataSource();
        //通过set方法进行赋值
        druid.setDriverClassName("com.mysql.jdbc.Driver");
        druid.setUrl("jdbc:mysql://localhost:3306/test");
        druid.setUsername("root");
        druid.setPassword("123456");
        //获取连接对象
        Connection connection = druid.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        druid.close();
    }
    @Test
    public void c3p0() throws Exception {
        //使用无参构造方法创建c3p0连接池对象
        ComboPooledDataSource c3p0=new ComboPooledDataSource();
        //通过set方法进行赋值
        c3p0.setDriverClass("com.mysql.jdbc.Driver");
        c3p0.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        c3p0.setUser("root");
        c3p0.setPassword("123456");
        //获取连接对象
        Connection connection = c3p0.getConnection();
        System.out.println(connection);
        //归还连接 关闭连接池
        connection.close();
        c3p0.close();
    }
    @Test
    public void u1(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
        Object u1 = applicationContext.getBean("u1");
        System.out.println(u1);
        applicationContext.close();
    }
}
